﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;
namespace WebGioiThieu.Areas.Admin.Controllers
{
    public class CommentManagerController : Controller
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();
        //
        // GET: /Admin/CommentManager/



        public ActionResult Index(string key, int? page)
        {
            int total = 0, pageSize = 5;
            int pageNumber = (page ?? 1);
            IEnumerable<Comment> comment = null;
            if (string.IsNullOrEmpty(key))
            {
                var comments = (from u in db.Comments
                                orderby u.TourID ascending
                                select u).ToList();
                comment = comments.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                total = comments.Count();
            }
            var pages = new PageInfo { PageSize = pageSize, TotalItem = total, Url = "/Admin/CommentManager/Index", Current = pageNumber };
            ViewBag.Page = pages;
            return View(comment);
        }

        public ActionResult TestComment(string key, int? page)
        {
            int total = 0, pageSize = 5;
            int pageNumber = (page ?? 1);
            IEnumerable<Comment> comment = null;
            if (string.IsNullOrEmpty(key))
            {
                var comments = (from u in db.Comments
                                where u.Publish == false
                                orderby u.TourID ascending
                                select u).ToList();
                comment = comments.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                total = comments.Count();
            }
            getLink(total, pageSize, pageNumber);
            return View(comment);
        }

        private void getLink(int total, int pageSize, int pageNumber)
        {
            var pages = new PageInfo { PageSize = pageSize, TotalItem = total, Url = "/Admin/CommentManager/TestComment", Current = pageNumber };
            ViewBag.Page = pages;
        }

        //
        // GET: /Admin/CommentManager/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/CommentManager/Create


        [ChildActionOnly]
        public ActionResult AddComment(int id)
        {
            var listComment = db.Comments.Where(c => c.TourID == id);
            return PartialView(listComment.ToList());
        }

        //
        // GET: /Admin/CommentManager/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Admin/CommentManager/Edit/5

        [HttpGet]
        public ActionResult publish(int id)
        {
            try
            {
                var comment = db.Comments.Find(id);
                if (comment != null)
                {
                    comment.Publish = true;
                }
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/CommentManager/Delete/5


        [HttpGet]
        public ActionResult Delete(int id)
        {
            try
            {
                Comment c = db.Comments.Find(id);
                db.Comments.Remove(c);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
